package com.cxm.support.config;
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;

import com.cxm.support.realm.AccountRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

//拦截器设置
@Configuration
public class ShiroConfig {
@Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager")DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean=new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager);
//权限设置
    Map<String,String> map=new HashMap<>();
//    map.put("/videomanagement/**","authc");
    map.put("/support/**","authc");

//    map.put("/videomanagement/videolistadd/**","perms[b]");
//    map.put("/administrator","roles[administrator]");

factoryBean.setFilterChainDefinitionMap(map);
//设置登录页面
    factoryBean.setLoginUrl("/denglu");
    //设置未授权页面
    factoryBean.setUnauthorizedUrl("/unauth");
        return factoryBean;
    }



    @Bean
    public DefaultWebSecurityManager securityManager(@Qualifier("accountRealm") AccountRealm accountRealm){

   DefaultWebSecurityManager manager=new DefaultWebSecurityManager();
    manager.setRealm(accountRealm);
   return  manager;

    }



    @Bean
    public AccountRealm accountRealm(){
return new AccountRealm();

    }
@Bean
    public ShiroDialect shiroDialect(){
    return new ShiroDialect();
    }









    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

    /**
     * 开启aop注解支持
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager")DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

























}
